package com.jrx.anytxn.transaction.controller.cms;


import com.jrx.anytxn.common.data.TxnRespCode;
import com.jrx.anytxn.common.exception.TxnBizException;
import com.jrx.anytxn.common.exception.TxnException;
import com.jrx.anytxn.common.uitl.BeanMapping;
import com.jrx.anytxn.common.uitl.JsonUtils;
import com.jrx.anytxn.customer.dto.customer.CustomerPageRes;
import com.jrx.anytxn.customer.service.ICustomerService;
import com.jrx.anytxn.feign.api.request.ApiContractBean;
import com.jrx.anytxn.feign.api.request.UserContractLogReq;
import com.jrx.anytxn.feign.api.response.TlUserContractLogListBean;
import com.jrx.anytxn.feign.api.response.UserContractLogRes;
import com.jrx.anytxn.feign.api.service.IUserApiService;
import com.jrx.anytxn.feign.gateway.bean.ApiResponse;
import com.jrx.anytxn.i18n.response.TxnPage;
import com.jrx.anytxn.i18n.response.TxnRespResult;
import com.jrx.anytxn.transaction.config.BasePathConfig;
import com.jrx.anytxn.transaction.dto.common.AccountMainInfoRes;
import com.jrx.anytxn.transaction.dto.common.LoanMainInfoRes;
import com.jrx.anytxn.transaction.dto.loan.AmWaiveRepaymentPlanInfoRes;
import com.jrx.anytxn.transaction.dto.loan.LoanAccountInfoRes;
import com.jrx.anytxn.transaction.dto.loan.LoanInformationRes;
import com.jrx.anytxn.transaction.dto.loan.TlLoanStagingMappingRes;
import com.jrx.anytxn.transaction.dto.payment.*;
import com.jrx.anytxn.transaction.dto.transactionLog.TlDisbursementLogRes;
import com.jrx.anytxn.transaction.dto.transactionLog.TlPaymentLogRes;
import com.jrx.anytxn.transaction.dto.transactionLog.TlPaymentMappingRes;
import com.jrx.anytxn.transaction.handler.IPaymentHandler;
import com.jrx.anytxn.transaction.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jrx.auth.filter.service.InfoService;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;


/**
 * @author liy
 * date  2019/8/15
 * @ClassName OrderController
 * 为客户提供查询订单相关数据
 * @Version V1.0
 **/
@Api(tags = "客户相关订单以及账户信息")
@RestController
@RequestMapping(value = "/transaction/customerOrder")
public class CustomerOrderController {
    private static final Logger logger = LoggerFactory.getLogger(CustomerOrderController.class);
    @Autowired
    private ICustomerOrderService customerOrderService;
    @Autowired
    private ICustomerService customerService;

    @Resource
    private InfoService infoService;
    @Resource
    private IUserApiService userApiService;
    @Autowired
    private IPaymentHandler paymentHandler;
    @Autowired
    private ILoanAccountMainInfoService loanAccountMainInfoService;
    @Autowired
    private ILoanMainInfoService loanMainInfoService;
    @Autowired
    private IWaiveRepaymentPlanInfoService waiveRepaymentPlanInfoService;
    @Autowired
    private ITlLoanStagingMappingService tlLoanStagingMappingService;
    @Autowired
    private BasePathConfig basePathConfig;
    @Autowired
    private IDisbursementLogService disbursementLogService;
    @Autowired
    private IPaymentLogService paymentLogService;
    @Autowired
    private IPaymentMappingService paymentMappingService;
    @Autowired
    private IThirdPartyPaymentService thirdPartyPaymentService;


    @ApiOperation(value = "贷款数据分页查询", notes = "贷款数据分页查询")
    @GetMapping(value = "getLoanMainWithPage")
    public TxnRespResult<TxnPage> getLoanMainWithPage(@RequestParam String channel, @RequestParam String customerId, @RequestParam String loanId, @RequestParam String loanType,
                                                      @RequestParam Integer pageNum, @RequestParam Integer pageSize, @RequestParam String beginDate, @RequestParam String endDate) {
        TxnRespResult<TxnPage> result = new TxnRespResult();
        String tenantId = String.valueOf(infoService.getUser().getProjectId());
        try {
            result = result.getSuccess(customerOrderService.getLoanMainWithPage(channel, customerId, tenantId, pageNum, pageSize, loanId, loanType, beginDate, endDate));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            result = result.getFail(TxnRespCode.BIZ_ERROR.getCode(), TxnRespCode.BIZ_ERROR.getUserTips());
        }
        return result;
    }

    @ApiOperation(value = "贷款计划查询", notes = "贷款计划查询")
    @GetMapping(value = "getRepaymentPlan")
    public TxnRespResult getRepaymentPlan(@RequestParam String customerId, @RequestParam String loanId) {
        TxnRespResult<ArrayList<AmRepaymentPlanRes>> result = new TxnRespResult();
        String tenantId = String.valueOf(infoService.getUser().getProjectId());
        try {
            result = customerOrderService.getRepaymentPlan(customerId, loanId, tenantId);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            result = result.getFail(TxnRespCode.BIZ_ERROR.getCode(), TxnRespCode.BIZ_ERROR.getUserTips());
        }
        return result;
    }

    @ApiOperation(value = "账户数据分页查询", notes = "账户数据分页查询")
    @GetMapping(value = "getAccountMainWithPage")
    public TxnRespResult<TxnPage> getAccountMainWithPage(@RequestParam String channel, @RequestParam String customerId, @RequestParam Integer pageNum,
                                                         @RequestParam Integer pageSize, @RequestParam String accountType, @RequestParam String txnCode,
                                                         @RequestParam String beginDate, @RequestParam String endDate) {
        TxnRespResult<TxnPage> result = new TxnRespResult();
        String tenantId = String.valueOf(infoService.getUser().getProjectId());
        try {
            result = result.getSuccess(customerOrderService.getAccountMainWithPage(channel, customerId, tenantId, pageNum, pageSize, accountType, txnCode, beginDate, endDate));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            result = result.getFail(TxnRespCode.BIZ_ERROR.getCode(), TxnRespCode.BIZ_ERROR.getUserTips());
        }
        return result;
    }

    @ApiOperation(value = "根据账户ID查询账户信息", notes = "根据账户ID查询账户信息")
    @GetMapping(value = "/getAccountMain/{accountId}/{customerId}")
    public TxnRespResult<AccountMainInfoRes> getAccountMainByAccountId(@PathVariable("accountId") String accountId, @PathVariable("customerId") String customerId) {
        TxnRespResult<AccountMainInfoRes> result = new TxnRespResult();
        String tenantId = String.valueOf(infoService.getUser().getProjectId());
        try {
            result = result.getSuccess(customerOrderService.getAmLoanMainInfoResByAccountId(tenantId, accountId, customerId));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            result = result.getFail(TxnRespCode.BIZ_ERROR.getCode(), TxnRespCode.BIZ_ERROR.getUserTips());
        }
        return result;
    }

    @ApiOperation(value = "根据订单id查询订单信息", notes = "根据订单id查询订单信息")
    @GetMapping(value = "/getLoanMain/{loanId}/{channel}/{customerId}")
    public TxnRespResult<ArrayList<LoanMainInfoRes>> getLoanMainInfoByLoanId(@PathVariable("loanId") String loanId, @PathVariable("customerId") String customerId, @PathVariable("channel") String channel) {
        TxnRespResult<ArrayList<LoanMainInfoRes>> result = new TxnRespResult();
        String tenantId = String.valueOf(infoService.getUser().getProjectId());
        try {
            result = result.getSuccess(customerOrderService.getLoanMainInfoByLoanId(tenantId, loanId, customerId, channel));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            result = result.getFail(TxnRespCode.BIZ_ERROR.getCode(), TxnRespCode.BIZ_ERROR.getUserTips());
        }
        return result;
    }

    @ApiOperation(value = "交易数据分页查询", notes = "交易数据分页查询")
    @GetMapping(value = "getTlTransactionInfoWithPage")
    public TxnRespResult<TxnPage> getTlTransactionInfoWithPage(@RequestParam String channel, @RequestParam String customerId, @RequestParam Integer pageNum, @RequestParam Integer pageSize,
                                                               @RequestParam String beginDate, @RequestParam String endDate) {
        TxnRespResult<TxnPage> result = new TxnRespResult();
        String tenantId = String.valueOf(infoService.getUser().getProjectId());
        try {
            result = result.getSuccess(customerOrderService.getTlTransactionInfoWithPage(channel, customerId, tenantId, pageNum, pageSize, beginDate, endDate));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            result = result.getFail(TxnRespCode.BIZ_ERROR.getCode(), TxnRespCode.BIZ_ERROR.getUserTips());
        }
        return result;
    }

    @ApiOperation(value = "根据交易参考号查询交易数据", notes = "根据交易参考号查询交易数据")
    @GetMapping(value = "getTlTransactionInfo/{txnNumber}/{channel}/{customerId}")
    public TxnRespResult<TlTransactionInfoRes> getTlTransactionInfo(@PathVariable("txnNumber") String txnNumber, @PathVariable("channel") String channel, @PathVariable("customerId") String customerId) {
        TxnRespResult<TlTransactionInfoRes> result = new TxnRespResult();
        String tenantId = String.valueOf(infoService.getUser().getProjectId());
        try {
            result = result.getSuccess(customerOrderService.getTlTransactionInfo(customerId, tenantId, channel, txnNumber));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            result = result.getFail(TxnRespCode.BIZ_ERROR.getCode(), TxnRespCode.BIZ_ERROR.getUserTips());
        }
        return result;
    }

    @ApiOperation(value = "根据订单号和期数查询交易数据", notes = "根据订单号和期数查询交易数据")
    @GetMapping(value = "getPlan/{loanId}/{channel}/{customerId}/{billingTenor}")
    public TxnRespResult<AmRepaymentPlanRes> getPlanInfo(@PathVariable("loanId") String loanId, @PathVariable("channel") String channel, @PathVariable("customerId") String customerId, @PathVariable("billingTenor") Integer billingTenor) {
        TxnRespResult<AmRepaymentPlanRes> result = new TxnRespResult();
        String tenantId = String.valueOf(infoService.getUser().getProjectId());
        try {
            result = result.getSuccess(customerOrderService.getOrderPlanByTenor(customerId, tenantId, channel, loanId, billingTenor));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            result = result.getFail(TxnRespCode.BIZ_ERROR.getCode(), TxnRespCode.BIZ_ERROR.getUserTips());
        }
        return result;
    }

    @ApiOperation(value = "根据用户查询合同数据", notes = "根据用户查询合同数据")
    @GetMapping(value = "getContractInfo")
    public TxnRespResult getContractInfo(@RequestParam String customerId, @RequestParam String channel, @RequestParam String loanId) {
        TxnRespResult result = new TxnRespResult();
        try {
            String tenantId = String.valueOf(infoService.getUser().getProjectId());
            String userId = customerService.getCustomerByChannelAndCustomerIdAndTensnId(channel, customerId, tenantId).getSecondLevel().getUserId();
            ApiContractBean apiContractBean = new ApiContractBean();
            apiContractBean.setTenantId(tenantId);
            apiContractBean.setUserId(userId);
            apiContractBean.setLoanId(loanId);
            String json = JsonUtils.obj2json(apiContractBean);
            ApiResponse<TlUserContractLogListBean> response = userApiService.getContractList(json);
            result = result.getSuccess(response.getData());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            result = result.getFail(TxnRespCode.BIZ_ERROR.getCode(), TxnRespCode.BIZ_ERROR.getUserTips());
        }
        return result;
    }

    @ApiOperation(value = "下载合同", notes = "下载合同")
    @GetMapping(value = "/downLoadContract")
    public void downLoadContract(HttpServletResponse response, @RequestParam("filePath") String filePath, @RequestParam("tenantId") String tenantId) {
        String rootPath = basePathConfig.getAgreeSavePath() + "/" + tenantId + "/";
        logger.info("下载合同地址：{}", filePath);
        response.setContentType("application/pdf");
        File file = new File(rootPath + filePath);
        try (FileInputStream fileInputStream = new FileInputStream(file);
             OutputStream outputStream = response.getOutputStream();) {
            IOUtils.write(IOUtils.toByteArray(fileInputStream), outputStream);
            response.setHeader("Content-Disposition", "inline; filename= file");
            outputStream.flush();
        } catch (Exception e) {
            logger.error("下载异常", e);
        }
    }

    @ApiOperation(value = "贷款数据分页查询", notes = "贷款数据分页查询")
    @GetMapping(value = "/getLoanByMobileOrIdNo")
    public TxnRespResult<TxnPage> getLoanByMobileOrIdNo(@RequestParam String mobile, @RequestParam String idNo, @RequestParam Integer pageNum, @RequestParam Integer pageSize) {
        TxnRespResult<TxnPage> result = new TxnRespResult();
        String tenantId = String.valueOf(infoService.getUser().getProjectId());
        try {
            List<CustomerPageRes> customerList = customerService.getCustomerList(mobile, idNo, tenantId, null, null, null);
            String custName = customerList.get(0).getChineseName();
            TxnPage<LoanMainInfoRes> res = customerOrderService.getLoanMainWithPage(null, customerList.get(0).getCustomerId(), tenantId, pageNum, pageSize,
                    null, null, null, null);
            res.getListData().forEach(loan -> loan.setCustName(custName));
            result = result.getSuccess(res);
        } catch (TxnException e) {
            logger.error(e.getMessage(), e);
            result = result.getFail(e.getErrCode(), e.getErrMsg());
        }
        return result;
    }

    @ApiOperation(value = "手工还款欠款查询", notes = "手工还款欠款查询")
    @GetMapping(value = "showHandlePayment")
    public TxnRespResult showHandlePayment(@RequestParam String customerId, @RequestParam String loanId, @RequestParam String channel) {
        TxnRespResult<ShowHandlePaymentRes> result = new TxnRespResult();
        String tenantId = String.valueOf(infoService.getUser().getProjectId());
        try {
            result = customerOrderService.showHandlePayment(customerId, loanId, tenantId, channel);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            result = result.getFail(TxnRespCode.BIZ_ERROR.getCode(), TxnRespCode.BIZ_ERROR.getUserTips());
        }
        return result;
    }

    @ApiOperation(value = "手工还款还款试算", notes = "手工还款还款试算")
    @GetMapping(value = "trailHandlePayment")
    public TxnRespResult trailHandlePayment(@RequestParam String customerId, @RequestParam String loanId, @RequestParam String channel, @RequestParam String paymentMethod, @RequestParam String repayAmount,
                                            @RequestParam String principalAmount) {
        TxnRespResult<ShowHandlePaymentRes> result = new TxnRespResult();
        String tenantId = String.valueOf(infoService.getUser().getProjectId());
        try {
            result = customerOrderService.trailHandlePayment(customerId, loanId, tenantId, channel, paymentMethod, repayAmount, principalAmount);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            result = result.getFail(TxnRespCode.BIZ_ERROR.getCode(), TxnRespCode.BIZ_ERROR.getUserTips());
        }
        return result;
    }

    @ApiOperation(value = "手工还款", notes = "手工还款")
    @PostMapping(value = "handlePayment")
    public TxnRespResult<PaymentRes> handlePayment(@RequestBody HandlePaymentReq handlePaymentReq) throws TxnException {
        String tenantId = String.valueOf(infoService.getUser().getProjectId());
        String userId = String.valueOf(infoService.getUser().getUserName());
        TxnRespResult<PaymentRes> result = paymentHandler.handlePayment(handlePaymentReq, tenantId, userId);
        return result;
    }

    @ApiOperation(value = "手工账单还款-跳过扣款步骤", notes = "手工账单还款-跳过扣款步骤")
    @PostMapping(value = "/handleBillSkipPay")
    public TxnRespResult<HandleSkipPayRes> handleSkipPay(@RequestBody HandleSkipPayReq handlePaymentReq) throws TxnException {
        String tenantId = String.valueOf(infoService.getUser().getProjectId());
        TxnRespResult<HandleSkipPayRes> result = paymentHandler.handleBillSkipPay(handlePaymentReq, tenantId);
        return result;
    }

    @ApiOperation(value = "手工提前结清-跳过扣款步骤", notes = "手工提前结清-跳过扣款步骤")
    @PostMapping(value = "/handlePreTerminationSkipPay")
    public TxnRespResult<HandleSkipPayRes> handlePreTerminationSkipPay(@RequestBody HandleSkipPayReq handlePaymentReq) throws TxnException {
        String tenantId = String.valueOf(infoService.getUser().getProjectId());
        TxnRespResult<HandleSkipPayRes> result = paymentHandler.handlePreTerminationSkipPay(handlePaymentReq, tenantId);
        return result;
    }

    @ApiOperation(value = "订单详情的账户信息查询list", notes = "订单详情的账户信息查询list")
    @GetMapping("/getLoanAccount/{loanId}/{customerId}")
    public TxnRespResult<ArrayList<LoanAccountInfoRes>> getBankCardList(@PathVariable String loanId, @PathVariable String customerId) {
        TxnRespResult<ArrayList<LoanAccountInfoRes>> result = new TxnRespResult<>();
        ArrayList<LoanAccountInfoRes> loanAccountInfoResList = new ArrayList<>();
        String tenantId = String.valueOf(infoService.getUser().getProjectId());
        try {
            LoanAccountInfoRes loanAccountInfoRes = BeanMapping.copy(loanAccountMainInfoService.getAmLoanAccountInfoByLoanIdAndCusId(loanId, customerId, tenantId), LoanAccountInfoRes.class);
            loanAccountInfoResList.add(loanAccountInfoRes);
            result.getSuccess(loanAccountInfoResList);
        } catch (TxnBizException e) {
            logger.error(e.getMessage(), e);
            result = result.getFail(TxnRespCode.BIZ_ERROR.getCode(), e);
        }
        return result;
    }

    @ApiOperation(value = "订单详情的账户信息明细查询", notes = "订单详情的账户信息明细查询")
    @GetMapping(value = "/getLoanAccountModel/{loanId}/{customerId}")
    public TxnRespResult<LoanAccountInfoRes> getPlanInfo(@PathVariable("loanId") String loanId, @PathVariable("customerId") String customerId) {
        TxnRespResult<LoanAccountInfoRes> result = new TxnRespResult();
        String tenantId = String.valueOf(infoService.getUser().getProjectId());
        try {
            LoanAccountInfoRes loanAccountInfoRes = BeanMapping.copy(loanAccountMainInfoService.getAmLoanAccountInfoByLoanIdAndCusId(loanId, customerId, tenantId), LoanAccountInfoRes.class);
            result = result.getSuccess(loanAccountInfoRes);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            result = result.getFail(TxnRespCode.BIZ_ERROR.getCode(), e);
        }
        return result;
    }

    @ApiOperation(value = "根据订单ID和客户ID查看利率参数信息", notes = "根据订单ID和客户ID查看利率参数信息")
    @GetMapping(value = "/getInterestModel/{loanId}/{customerId}")
    public TxnRespResult<LoanMainInfoRes> getInterestInfo(@PathVariable("loanId") String loanId, @PathVariable("customerId") String customerId) {
        TxnRespResult<LoanMainInfoRes> result = new TxnRespResult();
        String tenantId = String.valueOf(infoService.getUser().getProjectId());
        try {
            LoanMainInfoRes loanMainInfoRes = BeanMapping.copy(loanMainInfoService.getAmLoanMainInfoByLoanIdAndCusId(loanId, customerId, tenantId), LoanMainInfoRes.class);
            result = result.getSuccess(loanMainInfoRes);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            result = result.getFail(TxnRespCode.BIZ_ERROR.getCode(), e);
        }
        return result;
    }

    @ApiOperation(value = "订单详情的贴息还款计划查询list", notes = "订单详情的贴息还款计划查询list")
    @GetMapping("/getWaviceRepayMentPlanList/{loanId}/{customerId}")
    public TxnRespResult<List<AmWaiveRepaymentPlanInfoRes>> getWaviceRepayMentPlanList(@PathVariable String loanId, @PathVariable String customerId) {
        TxnRespResult<List<AmWaiveRepaymentPlanInfoRes>> result = new TxnRespResult<>();
        String tenantId = String.valueOf(infoService.getUser().getProjectId());
        try {
            result.getSuccess(waiveRepaymentPlanInfoService.getWaiveRepaymentListByLoanIdAndCusId(tenantId, customerId, loanId));
        } catch (TxnBizException e) {
            logger.error(e.getMessage(), e);
            result = result.getFail(TxnRespCode.BIZ_ERROR.getCode(), e);
        }
        return result;
    }

    @ApiOperation(value = "订单详情的贴息还款计划明细查询", notes = "订单详情的贴息还款计划明细查询")
    @GetMapping(value = "/getWaviceRepayMentPlanModel/{loanId}/{customerId}/{tenor}/{channel}")
    public TxnRespResult<AmWaiveRepaymentPlanInfoRes> getWaviceRepayMentPlanModel(@PathVariable("loanId") String loanId, @PathVariable("customerId") String customerId, @PathVariable("tenor") String tenor, @PathVariable("channel") String channel) {
        TxnRespResult<AmWaiveRepaymentPlanInfoRes> result = new TxnRespResult();
        String tenantId = String.valueOf(infoService.getUser().getProjectId());
        try {
            result = result.getSuccess(waiveRepaymentPlanInfoService.getWaiveRepaymentInfoByLoanIdAndCusId(tenantId, customerId, loanId, tenor, channel));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            result = result.getFail(TxnRespCode.BIZ_ERROR.getCode(), e);
        }
        return result;
    }

    @ApiOperation(value = "订单详情的分期关联订单查询list", notes = "订单详情的分期关联订单查询list")
    @GetMapping("/getLoanStagingMappingList/{customerId}/{loanId}")
    public TxnRespResult<List<TlLoanStagingMappingRes>> getLoanStagingMappingList(@PathVariable String customerId, @PathVariable String loanId) {
        TxnRespResult<List<TlLoanStagingMappingRes>> result = new TxnRespResult<>();
        String tenantId = String.valueOf(infoService.getUser().getProjectId());
        try {
            result.getSuccess(tlLoanStagingMappingService.getLoanStageListByLoanIdAndCusId(tenantId, customerId, loanId));
        } catch (TxnBizException e) {
            logger.error(e.getMessage(), e);
            result = result.getFail(TxnRespCode.BIZ_ERROR.getCode(), e);
        }
        return result;
    }

    @ApiOperation(value = "放款流水分页查询", notes = "放款流水分页查询")
    @GetMapping(value = "getDisbursementWithPage")
    public TxnRespResult<TxnPage> getDisbursementWithPage(@RequestParam String channel, @RequestParam String customerId, @RequestParam Integer pageNum,
                                                          @RequestParam Integer pageSize, @RequestParam String seqNo,
                                                          @RequestParam String beginDate, @RequestParam String endDate) {
        TxnRespResult<TxnPage> result = new TxnRespResult();
        String tenantId = String.valueOf(infoService.getUser().getProjectId());
        try {
            result = result.getSuccess(disbursementLogService.getDisbursementWithPage(channel, customerId, tenantId, pageNum, pageSize, seqNo, beginDate, endDate));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            result = result.getFail(TxnRespCode.BIZ_ERROR.getCode(), TxnRespCode.BIZ_ERROR.getUserTips());
        }
        return result;
    }

    @ApiOperation(value = "根据流水号查询放款流水信息", notes = "根据流水号查询放款流水信息")
    @GetMapping(value = "/getDisbursementBySeqNo/{seqNo}/{customerId}")
    public TxnRespResult<TlDisbursementLogRes> getDisbursementBySeqNo(@PathVariable("seqNo") String seqNo, @PathVariable("customerId") String customerId) {
        TxnRespResult<TlDisbursementLogRes> result = new TxnRespResult();
        String tenantId = String.valueOf(infoService.getUser().getProjectId());
        try {
            result = result.getSuccess(disbursementLogService.getTlDisbursementBySeqNoAndCusId(tenantId, seqNo, customerId));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            result = result.getFail(TxnRespCode.BIZ_ERROR.getCode(), TxnRespCode.BIZ_ERROR.getUserTips());
        }
        return result;
    }


    @ApiOperation(value = "还款流水分页查询", notes = "还款流水分页查询")
    @GetMapping(value = "getPaymentWithPage")
    public TxnRespResult<TxnPage> getPaymentWithPage(@RequestParam String channel, @RequestParam String customerId, @RequestParam Integer pageNum,
                                                     @RequestParam Integer pageSize, @RequestParam String seqNo,
                                                     @RequestParam String beginDate, @RequestParam String endDate) {
        TxnRespResult<TxnPage> result = new TxnRespResult();
        String tenantId = String.valueOf(infoService.getUser().getProjectId());
        try {
            result = result.getSuccess(paymentLogService.getPaymentWithPage(channel, customerId, tenantId, pageNum, pageSize, seqNo, beginDate, endDate));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            result = result.getFail(TxnRespCode.BIZ_ERROR.getCode(), TxnRespCode.BIZ_ERROR.getUserTips());
        }
        return result;
    }

    @ApiOperation(value = "根据流水号查询还款流水与订单关联表信息", notes = "根据流水号查询还款流水与订单关联表信息")
    @GetMapping(value = "/getPaymentMappingBySeqNo/{seqNo}/{customerId}")
    public TxnRespResult<List<TlPaymentMappingRes>> getPaymentMappingBySeqNo(@PathVariable("seqNo") String seqNo, @PathVariable("customerId") String customerId) {
        TxnRespResult<List<TlPaymentMappingRes>> result = new TxnRespResult();
        String tenantId = String.valueOf(infoService.getUser().getProjectId());
        try {
            result = result.getSuccess(paymentMappingService.getPaymentMappingBySeqNoAndCusId(tenantId, customerId, seqNo));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            result = result.getFail(TxnRespCode.BIZ_ERROR.getCode(), TxnRespCode.BIZ_ERROR.getUserTips());
        }
        return result;
    }

    @ApiOperation(value = "根据流水号查询还款流水信息", notes = "根据流水号查询还款流水信息")
    @GetMapping(value = "/getPaymentBySeqNo/{seqNo}/{customerId}")
    public TxnRespResult<TlPaymentLogRes> getPaymentBySeqNo(@PathVariable("seqNo") String seqNo, @PathVariable("customerId") String customerId) {
        TxnRespResult<TlPaymentLogRes> result = new TxnRespResult();
        String tenantId = String.valueOf(infoService.getUser().getProjectId());
        try {
            result = result.getSuccess(paymentLogService.getPaymentBySeqNoAndCusId(tenantId, seqNo, customerId));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            result = result.getFail(TxnRespCode.BIZ_ERROR.getCode(), TxnRespCode.BIZ_ERROR.getUserTips());
        }
        return result;
    }

    @ApiOperation(value = "协议文件分页查询", notes = "协议文件分页查询")
    @GetMapping(value = "getContractFileWithPage")
    public TxnRespResult<UserContractLogRes> getContractFileWithPage(@RequestParam String channel, @RequestParam String customerId, @RequestParam Integer pageNum,
                                                                     @RequestParam Integer pageSize, @RequestParam String contractType,
                                                                     @RequestParam String beginDate, @RequestParam String endDate) {
        TxnRespResult<UserContractLogRes> result = new TxnRespResult();
        String tenantId = String.valueOf(infoService.getUser().getProjectId());
        UserContractLogReq req = new UserContractLogReq();
        try {
            String userId = customerService.getCustomerByChannelAndCustomerIdAndTensnId(channel, customerId, tenantId).getSecondLevel().getUserId();
            req.setUserId(userId);
            req.setTenantId(tenantId);
            req.setPageNum(pageNum);
            req.setPageSize(pageSize);
            req.setBeginDate(beginDate);
            req.setEndDate(endDate);
            req.setContractType(contractType);
            String json = JsonUtils.obj2json(req);
            ApiResponse<UserContractLogRes> response = userApiService.getUserContractList(json);
            result = result.getSuccess(response.getData());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            result = result.getFail(TxnRespCode.BIZ_ERROR.getCode(), TxnRespCode.BIZ_ERROR.getUserTips());
        }
        return result;
    }


    @ApiOperation(value = "第三方还款流水分页查询", notes = "第三方还款流水分页查询")
    @GetMapping(value = "getThirdPartyPaymentWithPage")
    public TxnRespResult getThirdPartyPaymentWithPage(@RequestParam String channel, @RequestParam String customerId, @RequestParam Integer pageNum,
                                                      @RequestParam Integer pageSize, @RequestParam String seqNo,
                                                      @RequestParam String beginDate, @RequestParam String endDate) {
        TxnRespResult<TxnPage> result = new TxnRespResult<>();
        String tenantId = String.valueOf(infoService.getUser().getProjectId());
        try {
            result = result.getSuccess(thirdPartyPaymentService.getThirdPartyPaymentWithPage(channel, customerId, tenantId, pageNum, pageSize, seqNo, beginDate, endDate));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            result = result.getFail(TxnRespCode.BIZ_ERROR.getCode(), TxnRespCode.BIZ_ERROR.getUserTips());
        }
        return result;
    }

    @ApiOperation(value = "根据流水号查询第三方还款流水信息", notes = "根据流水号查询第三方还款流水信息")
    @GetMapping(value = "/getThirdPartyPaymentBySeqNo/{seqNo}/{customerId}")
    public TxnRespResult<ThirdPartyPaymentRes> getThirdPartyPaymentBySeqNo(@PathVariable("seqNo") String seqNo, @PathVariable("customerId") String customerId) {
        TxnRespResult<ThirdPartyPaymentRes> result = new TxnRespResult<>();
        String tenantId = String.valueOf(infoService.getUser().getProjectId());
        try {
            result = result.getSuccess(thirdPartyPaymentService.getThirdPartyPaymentBySeqNoAndCusId(tenantId, seqNo, customerId));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            result = result.getFail(TxnRespCode.BIZ_ERROR.getCode(), TxnRespCode.BIZ_ERROR.getUserTips());
        }
        return result;
    }

    @ApiOperation(value = "根据流水号查询还款流水与订单关联表信息", notes = "根据流水号查询还款流水与订单关联表信息")
    @GetMapping(value = "/getThirdPartyPaymentMappingByLoanId/{loanId}/{customerId}")
    public TxnRespResult<List<TlPaymentMappingRes>> getThirdPartyPaymentMappingByLoanId(@PathVariable("loanId") String loanId, @PathVariable("customerId") String customerId) {
        TxnRespResult<List<TlPaymentMappingRes>> result = new TxnRespResult();
        String tenantId = String.valueOf(infoService.getUser().getProjectId());
        try {
            result = result.getSuccess(paymentMappingService.getPaymentMappingByCusIdAndLoanId(tenantId, customerId, loanId));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            result = result.getFail(TxnRespCode.BIZ_ERROR.getCode(), TxnRespCode.BIZ_ERROR.getUserTips());
        }
        return result;
    }

    @ApiOperation(value = "在贷信息查询", notes = "在贷信息查询")
    @GetMapping(value = "/getLoanInformation")
    public TxnRespResult<ArrayList<LoanInformationRes>> getLoanInformation(@RequestParam String channel, @RequestParam String customerId) {
        TxnRespResult<ArrayList<LoanInformationRes>> result = new TxnRespResult();
        String tenantId = String.valueOf(infoService.getUser().getProjectId());
        try {
            result = result.getSuccess(customerOrderService.getLoanInformation(channel, customerId, tenantId));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            result = result.getFail(TxnRespCode.BIZ_ERROR.getCode(), TxnRespCode.BIZ_ERROR.getUserTips());
        }
        return result;
    }

    @ApiOperation(value = "根据订单id查询在贷信息", notes = "根据订单id查询在贷信息")
    @GetMapping(value = "/getLoanInfoByLoanId/{loanId}/{customerId}")
    public TxnRespResult<ArrayList<LoanInformationRes>> getLoanInfoByLoanId(@PathVariable("loanId") String loanId, @PathVariable("customerId") String customerId) {
        TxnRespResult<ArrayList<LoanInformationRes>> result = new TxnRespResult();
        String tenantId = String.valueOf(infoService.getUser().getProjectId());
        try {
            result = result.getSuccess(customerOrderService.getLoanInformationByLoanId(loanId, customerId, tenantId));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            result = result.getFail(TxnRespCode.BIZ_ERROR.getCode(), TxnRespCode.BIZ_ERROR.getUserTips());
        }
        return result;
    }
}
